const ProblemTypeModel = require('../db/models/problem_type');


class ProblemTypeService { 
  async addProblemType (data) {

    const type_title = data.type_title;
    const result = await ProblemTypeModel.findOne({ where: { type_title } });

    // //判断 check
    if (result) { 
      //更新 update  
      const id = result.get('id');
      return await ProblemTypeModel.update(data, {where: { id }})
    }else {
      //创建 create
      return await ProblemTypeModel.create(data);
    }
  }

  async getAllProblemType () {
    return await ProblemTypeModel.findAll();
  }

  async findProblemTypeById(id) {
    return await ProblemTypeModel.findOne({
      where: { id }
    })
  }

  async deleteProblemTypeById (id) {
    const result = await ProblemTypeModel.findOne({ where: { id } });
    return result.destroy();
  }
}

module.exports = new ProblemTypeService();